package com.xhtt.modules.app.controller.reportform;

import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.DateUtils;
import com.xhtt.common.utils.DoubleInfo;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.reportform.service.InsReportService;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 统计分析-巡检数据统计
 *
 * @Date 2019/3/15 14:32
 **/

@RestController
@RequestMapping(value = "app/report/inspection")
@Api(value = "统计分析-巡检数据统计", tags = "统计分析-巡检数据统计")
public class InsReportController {

    @Autowired
    private InsReportService insReportService;
    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "/everyone/list")
    @ApiOperation("统计分析-查询每人完成情况(web)")
    @Login
    public R getInsEveryone(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insReportService.queryInsEveryone(params));
    }

    @GetMapping(value = "/everyone/export")
    @ApiOperation("统计分析-查询每人完成情况导出")
    @Login
    public void exportEveryone(HttpServletResponse response, @RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        insReportService.exportEveryone(response, params);
    }

    @GetMapping(value = "/everyTeam/list")
    @ApiOperation("统计分析-查询每班完成情况(web)")
    @Login
    public R getInsEveryTeam(@RequestParam Map<String, Object> params) {
        return R.ok().put(Constant.DATA, insReportService.queryInsEveryTeam(params));
    }

    @GetMapping(value = "/everyTeam/export")
    @ApiOperation("统计分析-查询每班完成情况导出")
    @Login
    public void exportEveryTeam(HttpServletResponse response, @RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        insReportService.exportEveryTeam(response, params);
    }

    @GetMapping(value = "/everyday/list")
    @ApiOperation("统计分析-安全巡检(app)")
    @Login
    public R getIns(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {

        if (!Optional.ofNullable(params.get("datetime")).isPresent()) {
            params.put("datetime", DateUtils.dtf.format(DateUtils.now()));
        } else {
            String datetime = (String) params.get("datetime");
            params.put("datetime", datetime + " 00:00:00");
        }

        List<Integer> userIds = sysUserService.queryAuth(user);
        params.put("userIds", userIds);

        DoubleInfo<Integer, Integer> doubleInfo = insReportService.queryEverydayInsCase(params);
        R r = new R();
        r.put(Constant.DATA, insReportService.queryEverydayTotal(params));
        r.put("finished", doubleInfo.getSecond());
        r.put("unfinished", doubleInfo.getFirst());
        return r;
    }
}
